package pikes.util;

import java.io.IOException;
import java.util.Collection;

import pikes.core.Printable;

/**
 * Utility helper class for all Pikes modules.
 * @author Peter Bona
 */
public final class Util {

	private Util() {
		// left intentionally empty
	}

	/**
	 * Checks if the parameter charsequences are the same.
	 * @param string1
	 * @param string2
	 * @return True if the two charsequences have the same characters in the same order.
	 */
	public static boolean equals(final CharSequence string1, final CharSequence string2) {
		if ((string1 == null) && (string2 == null)) {
			return true;
		}

		if ((string1 == null) && (string2 != null)) {
			return false;
		}

		if ((string2 == null) && (string1 != null)) {
			return false;
		}

		if (string1.length() != string2.length()) {
			return false;
		}

		for (int i = 0; i < string1.length(); i++) {
			if (string1.charAt(i) != string2.charAt(i)) {
				return false;
			}
		}

		return true;
	}

	/**
	 * Checks if two printable objects produce the same character content
	 * @param printable1
	 * @param printable2
	 * @return true if the character contents are the same, false otherwise
	 */
	public static boolean equals(final Printable printable1, final Printable printable2) {
		if ((printable1 == null) && (printable2 == null)) {
			return true;
		}

		if ((printable1 == null) && (printable2 != null)) {
			return false;
		}

		if ((printable1 == null) && (printable2 != null)) {
			return false;
		}

		StringBuilder builder1 = new StringBuilder();
		try {
			printable1.print(builder1);
		}
		catch (IOException e) {
			builder1 = new StringBuilder();
			builder1.append(e);
		}

		StringBuilder builder2 = new StringBuilder();
		try {
			printable2.print(builder2);
		}
		catch (IOException e) {
			builder2 = new StringBuilder();
			builder2.append(e);
		}

		return equals(builder1, builder2);
	}

	/**
	 * Checks if the string provided as CharSequence has a valid text, meaning it cannot be null, cannot be empty and
	 * has to contain at least one non-whitespace character.
	 * @param string
	 * @return True if the character sequences has at least one non whitespace character.
	 */
	public static boolean hasText(final CharSequence string) {
		if (string == null) {
			return false;
		}

		if (string.length() == 0) {
			return false;
		}
		for (int i = 0; i < string.length(); i++) {
			if (!Character.isWhitespace(string.charAt(i))) {
				return true;
			}
		}

		return false;
	}

	/**
	 * Checks if the printable object's character content is empty
	 * @param printable
	 * @return True if the printed character sequences has at least one non whitespace character.
	 */
	public static boolean hasText(final Printable printable) {
		if (printable == null) {
			return false;
		}

		StringBuilder builder = new StringBuilder();
		try {
			printable.print(builder);
			return hasText(builder);
		}
		catch (IOException e) {
			return false;
		}
	}

	/**
	 * Checks if the collection is empty by checking first if it is not null, the calling isEmpty method of the
	 * collection
	 * @param collection
	 * @return true if not null and isEmpty returns true, false otherwise
	 */
	public static boolean isEmpty(final Collection<?> collection) {
		if (collection == null) {
			return true;
		}

		return collection.isEmpty();
	}
}
